package com.spd.shop.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.spd.shop.service.ITenantService;
import com.spd.shop.entity.Tenant;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 孙沛栋
 * @since 2023-03-17
 */
@RestController
@RequestMapping("/tenant")
public class TenantController {

    @Resource
    private ITenantService tenantService;

    //新增或者更新
    @PostMapping
    public boolean save(@RequestBody Tenant tenant) {
        return tenantService.saveOrUpdate(tenant);
    }

    //删除
    @DeleteMapping("/{id}")
    public Boolean delete(@PathVariable Integer id) {
        return tenantService.removeById(id);
    }

    //批量删除
    @PostMapping("/del/batch")
    public boolean deleteBatch(@RequestBody List<Integer> ids) {
        return tenantService.removeBatchByIds(ids);
    }

    //查询所有数据
    @GetMapping
    public List<Tenant> findAll() {
        return tenantService.list();
    }

    //根据id查询
    @GetMapping("/{id}")
    public Tenant findOne(@PathVariable Integer id) {
        return tenantService.getById(id);
    }

    //分页查询
    @GetMapping("/page")
    public Page<Tenant> findPage(@RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize,
                                 @RequestParam(defaultValue = "") String tenantNum,
                                 @RequestParam(defaultValue = "") String tenantName,
                                 @RequestParam(defaultValue = "") String phone) {
        QueryWrapper<Tenant> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (!"".equals(tenantNum)) {
            queryWrapper.like("tenant_num", tenantNum);
        }
        if (!"".equals(tenantName)) {
            queryWrapper.like("tenant_name", tenantName);
        }
        if (!"".equals(phone)) {
            queryWrapper.like("phone", phone);

        }
        return tenantService.page(new Page<>(pageNum, pageSize), queryWrapper);
    }


}

